//
//  CAStartPageController.m
//  cascade-realm
//
//  Created by QSC on 2024/5/17.
//

#import "CAStartPageController.h"

#import "PictureCell.h"
@interface CAStartPageController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSArray * imageArr;
@end

@implementation CAStartPageController
- (NSArray *)imageArr{
    
    if(_imageArr == nil){
        
        _imageArr = @[
            Img(@"start1"),Img(@"start2"),Img(@"start3"),
        ];
    }
    return _imageArr;
}

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(ScreenW, ScreenH);
        
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[PictureCell class] forCellWithReuseIdentifier:@"PictureCell"];
        // 去掉滚动条
        collectionView.showsHorizontalScrollIndicator = NO;
        // 分页
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        _collectionView = collectionView;
    }
    
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.imageArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    PictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCell" forIndexPath:indexPath];
    
    cell.imageSource = self.imageArr[indexPath.item];
    
    cell.index = indexPath.item;
    
    WeakSelf
    cell.nextBlock = ^{
        [weakSelf.collectionView setContentOffset:CGPointMake(ScreenW * (indexPath.row + 1), 0) animated:YES];
    };
    
    return cell;
}


@end
